iT邦幫忙

2026 iThome 鐵人賽

DAY 4
0
Software Development

用python做一個X11 WM系列 第 4

Day 4|顯示鼠標(我要瘋了

  • 分享至 

  • xImage
  •  

為什麼光是一個顯示鼠標就發文呢?因為沒有api參考 我直接翻python-xlib的原碼(.venv/lib/python3.13/site-packages/Xlib/)快瘋了 c的xlib有XCreateFontCursor,所以我試dpy.create_font_cursor()root.create_font_cursor()
結果都是AttributeError: create_font_cursor
ai給我:

font = dpy.open_font('cursor')           # 開啟 cursor 字型
    cursor = font.create_glyph_cursor(
        font,                                # mask 用同一個 font
        Xcursorfont.left_ptr,               # 游標形狀(source_char)
        Xcursorfont.left_ptr + 1,           # mask_char(通常是 +1)
        (0, 0, 0),                          # 前景色 RGB(黑色)
        (65535, 65535, 65535)               # 背景色 RGB(白色)
    )
    root.change_attributes(cursor=cursor)
    dpy.flush()

但也會報錯 找不到Xcursorfont.left_ptr常數,下面是我改的:

font = dpy.open_font('cursor')           # 開啟 cursor 字型
cursor = font.create_glyph_cursor(
    font,                                # mask 用同一個 font
    68,                                  # 游標形狀(source_char)https://xorg.freedesktop.org/archive/current/doc/libX11/libX11/libX11.html#x_font_cursors
    68 + 1,                              # mask_char(通常是 +1)
    (0, 0, 0),                           # 前景色 RGB(黑色)
    (65535, 65535, 65535)                # 背景色 RGB(白色)
)
root.change_attributes(cursor=cursor)

68是一般左斜鼠標的常數,至於68+1的mask_char我不知道是什麼


上一篇
Day 3|基本Xlib結構(?
下一篇
Day 5|還沒開始寫就在debug
系列文
用python做一個X11 WM14
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言